import { Effect } from '@/models/connect';
import { Reducer } from 'react';
import apis from '@/services';
import { SimpleResponse } from '@/utils/common';


export interface FilesModelState {
    result: any;
}
export interface FilesModelType {
    namespace: string;
    state: FilesModelState;
    effects: {
      query: Effect;
      remove:Effect;
      update:Effect
    };
    reducers: {
      save: Reducer<any, any>;
    };
  }
const FilesModel: FilesModelType = {
    namespace: 'files',
    state: {
        result: {},
    },
    effects: {
        *query({ payload }, { call, put }) {
            const response: any = yield call(apis.files.list, payload);
            yield put({
              type: 'save',
              payload: response.result,
            });
        },
        *remove({ payload, callback }, { call, put }) {
          const response: SimpleResponse = yield call(apis.files.remove, payload);
          callback(response);
        },
        *update({ payload, callback }, { call }) {
          const response: SimpleResponse = yield call(apis.files.update, payload);
          callback(response);
        },

    },
    reducers: {
        save(state, action) {
            return {
            ...state,
            result: action.payload,
            };
        },
    },
}
export default FilesModel